////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2010 Leon Coto Rodriguez
//  leoncoto@gmail.com
//  
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//  
//   The above copyright notice and this permission notice shall be
//   included in all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package org.flexspeedup.services.amf 
{
import org.flexspeedup.services.ICollectionService;

import mx.rpc.AsyncToken;
import mx.rpc.remoting.mxml.RemoteObject;

/**
 * A concrete implementation of ICollectionService to use as a base class for 
 * AMF based collection services
 */
public dynamic class AbstractCollectionService 
                                       extends mx.rpc.remoting.mxml.RemoteObject 
                                       implements ICollectionService
{

    //--------------------------------------------------------------------------
    //
    // Class constants
    //
    //--------------------------------------------------------------------------

    //--------------------------
    // CREATE_ITEM_OPERATION
    //--------------------------
    /**
     * The default name for the remote operation corresponding to the 
     * <code>createItem</code> method in this class.
     */
    public static const CREATE_ITEM_OPERATION:String = "createItem";

    //--------------------------
    // UPDATE_ITEM_OPERATION
    //--------------------------
    /**
     * The default name for the remote operation correspoinding to the 
     * <code>updateItem</code> method in this class.
     */
    public static const UPDATE_ITEM_OPERATION:String = "updateItem";

    //--------------------------
    // FETCH_RESULT_SET_OPERATION
    //--------------------------
    /**
     * The default value for the remote operation corresponding to the 
     * <code>fetchResultSet</code> method in this class.
     */
    public static const FETCH_RESULT_SET_OPERATION:String = "fetchResultSet";
    
    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new AbstractCollectionService. Meant to always be a superclass.
     */
    public function AbstractCollectionService(destination:String = null)
    {
        super(destination);
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    // createItemOperation
    //----------------------------------
    [Bindable]
    /**
     * The name the remote operation corresponding to the 
     * <code>createItem</code> in this class, for a concrete class 
     * implementation.
     * 
     * @default "createItem"
     */
    public var createItemOperation:String =
        AbstractCollectionService.CREATE_ITEM_OPERATION;

    //----------------------------------
    // updateItemOperation
    //----------------------------------
    [Bindable]
    /**
     * The name for the remote operation corresponding to the 
     * <code>updateItem</code> method in this class, for a concrete 
     * implementation. 
     * 
     * @default "updateItem".
     */
    public var updateItemOperation:String = 
        AbstractCollectionService.UPDATE_ITEM_OPERATION;
        
    //----------------------------------
    // fetchResultSetOperation
    //----------------------------------
    /**
     * The name for the remote operation corresponding to the 
     * <code>fetchResultSet</code> method in this class, for a concrete 
     * implementation. 
     * 
     * @default "fetchResultSet".
     */
    public var fetchResultSetOperation:String = 
        AbstractCollectionService.FETCH_RESULT_SET_OPERATION;
   
    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     * Concrete abstract implementation.
     * @inheritDoc org.flexspeedup.services.ICollectionService#createItem
     */
    public function createItem(item:*):AsyncToken
    {
        return getOperation(createItemOperation).send(item);
    }

    /**
     * Concrete abstract implementation.
     * @inheritDoc org.flexspeedup.services.ICollectionService#updateItem
     */
    public function updateItem(item:*):AsyncToken
    {
        return getOperation(updateItemOperation).send(item);
    }

    /**
     * Concrete abstract implementation.
     * @inheritDoc org.flexspeedup.services.ICollectionService#fetchResultSet
     */
     public function fetchResultSet(pageNumber:int, pageSize:int):AsyncToken
     {
        return getOperation(fetchResultSetOperation).send(pageNumber, pageSize);
     }
}
}